package com.crm.vo;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import com.crm.commons.DataEnum;
import com.crm.entity.CrmUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

public class MySecurityUser implements UserDetails {
    private static final Logger log = LoggerFactory.getLogger(MySecurityUser.class);
    private final CrmUser sysUser;
    public MySecurityUser(CrmUser sysUser,List<GrantedAuthority> list){
        this.sysUser=sysUser;
        this.list = list;
    }

    public CrmUser getSysUser() {
        return sysUser;
    }

    private List<GrantedAuthority> list;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //没有配置权限
        return list;
    }

    @Override
    public String getPassword() {
        return sysUser.getLoginPwd();
    }

    @Override
    public String getUsername() {
        return this.sysUser.getName();
    }

    @Override
    public boolean isAccountNonExpired() {
        boolean before = DateTime.now().isBefore(DateUtil.parseDate(sysUser.getExpireTime()));
        Console.log(DateTime.now(),DateUtil.parseDate(sysUser.getExpireTime()),before);
        return before;
    }

    @Override
    public boolean isAccountNonLocked() {
        return sysUser.getLockState().equals(DataEnum.NORMAL.getCode());
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return DateTime.now().isBefore(DateUtil.parseDate(sysUser.getExpireTime()));
    }

    @Override
    public boolean isEnabled() {
        return sysUser.getLockState().equals(DataEnum.NORMAL.getCode());
    }
}